package com.jonluo.controller;

import com.alibaba.fastjson.JSONObject;
import com.jonluo.common.utils.EnvVariable;
import com.jonluo.common.utils.R;
import com.jonluo.common.utils.UuidUtils;
import com.jonluo.entity.DeptEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * 部门管理
 * @author com.jonluo
 * Date 2017/9/15
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends AbstractController {

	@Autowired
	RestTemplate restTemplate;
	@Autowired
	EnvVariable envVariable;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public List<DeptEntity> list(){
		return  getSubDetpList(getCurrentDeptId(),true);
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:dept:select")
	public R select(){

		List<DeptEntity> deptList = list();
		//超级管理员可以添加一级部门根节点
		if(envVariable.getSuperAdminId().equals(getCurrentUserId())){
			DeptEntity root = new DeptEntity();
			root.setDeptId("0");
			root.setName("一级部门");
			root.setParentId("-1");
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public R info(){
		DeptEntity dept = getDeptById(getCurrentDeptId());
		//一级部门id为0
		return R.ok().put("deptId", dept.getParentId());
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public R info(@PathVariable("deptId") String deptId){
		DeptEntity dept = getDeptById(deptId);
		return R.ok().put("dept", dept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public R save(@RequestBody DeptEntity dept){
		dept.setDeptId(UuidUtils.getUUID("dept"));
		String resultJson = restTemplate.postForEntity(envVariable.getBaseUrl()+"/dept/add",dept, String.class).getBody();
		JSONObject resultMap = JSONObject.parseObject(resultJson);
		if(0 == resultMap.getInteger("code")){
			return R.ok("success");
		}else {
			return R.error("fail");
		}
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:dept:update")
	public R update(@RequestBody DeptEntity dept){
		String resultJson = restTemplate.postForEntity(envVariable.getBaseUrl()+"/dept/update",dept, String.class).getBody();
		JSONObject resultMap = JSONObject.parseObject(resultJson);
		if(0 == resultMap.getInteger("code")){
			return R.ok("success");
		}else {
			return R.error("fail");
		}
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dept:delete")
	public R delete(String deptId){
		//判断是否有子部门
		List<DeptEntity> deptList = getSubDetpList(deptId,false);
		if(deptList.size() > 0){
			return R.error("请先删除子部门");
		}
		JSONObject json = new JSONObject();
		json.put("ids", deptId);
		String resultJson = restTemplate.postForEntity(envVariable.getBaseUrl()+"/dept/deleteByIds",json, String.class).getBody();
		JSONObject resultMap = JSONObject.parseObject(resultJson);
		if(0 == resultMap.getInteger("code")){
			return R.ok("success");
		}else {
			return R.error("fail");
		}
	}


	public DeptEntity getDeptById(String id){
		DeptEntity dept = null ;
		String resultJson = restTemplate.getForEntity(envVariable.getBaseUrl()+"/dept/getById/"+id, String.class).getBody();
		JSONObject resultMap = JSONObject.parseObject(resultJson);
		if ( 0 == resultMap.getInteger("code")){
			dept = resultMap.getObject("data",DeptEntity.class);
		}else {
			dept = new DeptEntity();
		}
		return dept;
	}

	/**
	 * 获取下级单位
	 * @param parentId
	 * @param isAll
     * @return
     */
	public List<DeptEntity> getSubDetpList(String parentId , boolean isAll){
		JSONObject json = new JSONObject();
		json.put("isAll",isAll);
		json.put("parentId",parentId);
		String resultJson = restTemplate.postForEntity(envVariable.getBaseUrl()+"/dept/getSubDetpList",json, String.class).getBody();
		JSONObject resultMap = JSONObject.parseObject(resultJson);
		List<DeptEntity> deptList = (List<DeptEntity>) resultMap.get("data");
		return deptList;
	}

}
